package com.chat.view;

import com.chat.Local;
import com.chat.service.*;
import javafx.application.Platform;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.text.Text;

import javax.swing.*;

import java.util.Objects;

import static com.chat.view.VerifyMessageController.ver;

/*
    listview中存放验证消息的Controller类
 */
public class VerifyListController {
    public Text id;

    public Text nickname;

    public String userid;

    public String state;

    public void setState(String state) {
        this.state = state;
    }

    public void setId(String id) {
        this.id.setText(id);
        this.userid=id;
    }

    public void setNickname(String nickname) {
        this.nickname.setText(nickname);
    }

    public void accept(){
        if(Objects.equals(state, "0")) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("提示");
            alert.setHeaderText("恭喜");
            alert.setContentText("您已通过对方的申请！");
            alert.showAndWait();
            AddFriend af = new AddFriend();
            af.addfriend(Local.user.getUsername(), userid);
        }//判断接受好友申请的情况
        else if(Objects.equals(state, "1")){
            AddGroupMember agm=new AddGroupMember();
            agm.addgroupmember(Local.group.getId(),userid,"1");
        }//判断接受群聊申请的情况
    }

    public void refuse(){
        if(Objects.equals(state, "0")) {
            RefuseFriend rf = new RefuseFriend();
            rf.refusefriend(Local.user.getUsername(), userid);
            JOptionPane.showMessageDialog(null, "已拒绝对方的好友申请");
            Platform.runLater(() -> {
                ((javafx.scene.control.ListView) VerifyMessageView.$("verify")).getItems().clear();
            });
            ver.clear();
            GetVerifyMessage gvm = new GetVerifyMessage();
            gvm.getverifymeesage();
        }//拒绝好友申请的情况
        else if(Objects.equals(state, "1")){
            RefuseGroupVerify rgv=new RefuseGroupVerify();
            rgv.refusegroupverify(Local.group.getId(),userid);
        }//拒绝群聊申请的情况
    }

}
